$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'noc/nocbusiness/list',
        datatype: "json",
        colModel: [
            { label: 'id', name: 'id', index: 'id', width: 50, key: true },
            { label: '姓名', name: 'name', index: 'name', width: 80 },
            { label: '手机号', name: 'mobile', index: 'mobile', width: 110 },
            { label: '参展单位', name: 'company', index: 'company', width: 80 },
            { label: '职位', name: 'position', index: 'position', width: 80 },
            { label: '部门', name: 'department', index: 'department', width: 80 },
            { label: '主营产品', name: 'mainProduct', index: 'main_product', width: 80 },
            { label: '是否负责人', name: 'isman', index: 'isman', width: 80 ,formatter: function(value, options, row){
                    return value === '1' ? '是' : '否';
                }},
            { label: '二维码', name: 'ercode', index: 'ercode', width: 120 },
            { label: '链接', name: 'url', index: 'url', width: 300 },
            { label: '允许最大同行人数', name: 'txMax', index: 'tx_max', width: 140 },
            { label: '第一天啤酒节门票总数', name: 'mpMax', index: 'mp_max', width: 130 },
            { label: '第二天啤酒节门票总数', name: 'mpMax1', index: 'mp_max1', width: 130 },
            { label: '第三天啤酒节门票总数', name: 'mpMax2', index: 'mp_max2', width: 130 },
            { label: '业务员', name: 'ly', index: 'ly', width: 80 },
            { label: '展会ID', name: 'tid', index: 'tid', width: 80 },
            { label: '签到状态', name: 'isSign', index: 'is_sign', width: 80,formatter: function(value, options, row){
                    return value === '1' ? '是' : '否';
                } },
            { label: '到场时间', name: 'signTime', index: 'sign_time', width: 180 },
            { label: '创建时间', name: 'createTime', index: 'create_time', width: 180 },
            { label: '修改者', name: 'updator', index: 'updator', width: 80 },
            { label: '修改时间', name: 'updateTime', index: 'update_time', width: 180 }

        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        shrinkToFit:false,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            //$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        },
        beforeSelectRow: beforeSelectRow,//js方法
    });
});

//单选设置
function beforeSelectRow()
{
    $("#jqGrid").jqGrid('resetSelection');
    return(true);
}

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            mobile: null,
            company: null,
            isman: ""
        },
        showList: true,
        showAdd: false,
        showFpseat: false,
        showFpseatAdd: false,
        title: null,
        nocBusiness: {},
        templates:[],
        templateId: "",
        nocBeerSeat: {},
        nocRow:[],
        nocNum:[],
        nocSeat:[],
        checkSeats:[],
        area:"",
        table_row:"",
        table_num:"",
        nocLabels: [],
        selectedLabels:[],
        mpNum:0,
    },
    watch: {
        area (val,oldval){
            this.getRow();
        },
        table_row(val,oldval){
            this.getNum();
        },
        table_num(val,oldval){
            this.getSeat();
        }
    },
    created(){
        this.getTemplates();
        this.getLabels();
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            console.log(vm.templateId);
            if(!vm.templateId){
                alert("请选择展会！");
                return;
            }
            var grid = $("#jqGrid");
            var id = grid.getGridParam("selrow");
            if (id == null) {
                vm.showAdd = true;
                vm.showList = false;
                vm.title = "新增";
                vm.nocBusiness = {};
                vm.nocBusiness.isman = '1';
            } else {
                vm.showAdd = true;
                vm.showList = false;
                vm.title = "新增";
                $.get(baseURL + "noc/nocbusiness/info/"+id, function(r){
                    vm.nocBusiness = {};
                    vm.nocBusiness.isman = '0';
                    vm.nocBusiness.txid = r.txid;
                    vm.nocBusiness.company = r.nocBusiness.company;
                    vm.nocBusiness.tid = r.nocBusiness.tid;
                });
            }



            /*vm.showList = false;
            vm.title = "新增";
            vm.nocBusiness = {};*/

            //vm.getTemplates();
        },
        update: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.showAdd = true;
            vm.showList = false;

            //配对标签取消选择
            $("input[type='checkbox'][id='cklabel']").removeAttr("checked");//取消全选

            vm.title = "修改";
            vm.getInfo(id);
            vm.getTemplates();


        },
        saveOrUpdate: function (event) {
            if(vm.validator()){
                return ;
            }

            $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                vm.nocBusiness.tid = vm.templateId;
                var url = vm.nocBusiness.id == null ? "noc/nocbusiness/save" : "noc/nocbusiness/update";

                var labelIds = "";
                $("input[type='checkbox'][id='cklabel']").each(function() {
                    if ($(this).is(":checked")) {
                        labelIds += $(this).val()+",";
                    }
                });
                vm.nocBusiness.labelIds = labelIds;

                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.nocBusiness),
                    success: function(r){
                        if(r.code === 0){
                            layer.msg("操作成功", {icon: 1});
                            vm.reload();
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
            });
        },
        saveOrUpdate2: function (event) {
            id = getSelectedRow();
            console.log(id);
            var ids = "";
            $("input[type='checkbox'][id='ckseat']").each(function() {
                if ($(this).is(":checked")) {
                    ids += $(this).val()+",";
                }
            });
            console.log(ids);
            $('#btnSaveOrUpdate2').button('loading').delay(1000).queue(function() {
                $.ajax({
                    type: "GET",
                    url: baseURL + 'noc/nocbeerseat/save',
                    contentType: "application/json",
                    data: {ids:ids,businessId:id},
                    success: function(r){
                        if(r.code === 0) {
                            vm.getSeat();
                            layer.msg("操作成功", {icon: 1});
                            var page = $("#jqGrid2").jqGrid('getGridParam','page');
                            $("#jqGrid2").jqGrid('setGridParam',{
                                page:page
                            }).trigger("reloadGrid");
                            $('#btnSaveOrUpdate2').button('reset');
                            $('#btnSaveOrUpdate2').dequeue();
                        }else {
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate2').button('reset');
                            $('#btnSaveOrUpdate2').dequeue();
                        }
                    }
                });
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                if(!lock) {
                    lock = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "noc/nocbusiness/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
                    });
                }
            }, function(){
            });
        },
        del2: function (event) {
            var grid = $("#jqGrid2");
            var rowKey = grid.getGridParam("selrow");
            if(!rowKey){
                alert("请选择一条记录");
                return ;
            }
            var ids = grid.getGridParam("selarrrow");
            if(ids == null){
                return ;
            }
            var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                if(!lock) {
                    lock = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "noc/nocbeerseat/remove",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                vm.getSeat();
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid2").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
                    });
                }
            }, function(){
            });
        },
        list2: function (id) {
            console.log(id);
            $("#jqBlock").html("");
            $("#jqBlock").append('<table id="jqGrid2"></table>');
            $("#jqBlock").append('<div id="jqGridPager2"></div>');
            $("#jqGrid2").jqGrid({
                url: baseURL + 'noc/nocbeerseat/list?business_id='+id,
                datatype: "json",
                colModel: [
                    { label: 'id', name: 'id', index: 'id', width: 50, key: true },
                    { label: '日期', name: 'validDate', index: 'valid_date', width: 180 },
                    { label: '区域', name: 'area', index: 'area', width: 80 },
                    { label: '排号', name: 'tableRow', index: 'table_row', width: 80 },
                    { label: '桌号', name: 'tableNum', index: 'table_num', width: 80 },
                    { label: '座位号', name: 'seatNum', index: 'seat_num', width: 80 },
                    { label: '绑定', name: 'fid', index: 'fid', width: 80 }
                ],
                viewrecords: true,
                height: 385,
                rowNum: 10,
                rowList : [10,30,50],
                rownumbers: true,
                rownumWidth: 25,
                autowidth:true,
                multiselect: true,
                pager: "#jqGridPager2",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                gridComplete:function(){
                    //隐藏grid底部滚动条
                    $("#jqGrid2").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                }
            });

        },
        getInfo: function(id){
            $.get(baseURL + "noc/nocbusiness/info/"+id, function(r){
                vm.nocBusiness = r.nocBusiness;
                vm.selectedLabels = r.labels;
                console.log(r.labels)
                vm.getLabels();
            });
        },
        reload: function (event) {
            vm.showList = true;
            vm.showAdd = false;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'mobile': vm.q.mobile,'company':vm.q.company,'templateId':vm.templateId,'isman':vm.q.isman},
                page:page
            }).trigger("reloadGrid");
        },
        back: function () {
            vm.showFpseat = false;
            vm.showFpseatAdd = false;
            vm.showAdd = false;
            vm.showList = true;
        },
        getTemplates: function () {
            $.ajax({
                type: "POST",
                url: baseURL + "noc/noctemplate/listByRole",
                contentType: "application/json",
                data: {},
                success: function(r){
                    if(r.code == 0){
                        vm.templates = r.list;
                    }
                    vm.templateId = vm.nocBusiness.tid;
                }
            });
        },
        validator: function () {
            if(isBlank(vm.nocBusiness.name)){
                alert("姓名不能为空");
                return true;
            }

            if(isMobile(vm.nocBusiness.mobile)){
                alert("手机号格式格式不正确");
                return true;
            }

            if(isBlank(vm.nocBusiness.company)){
                alert("单位不能为空");
                return true;
            }
        },
        fpseat:function () {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var rowData = $("#jqGrid").jqGrid('getRowData',id);
            if(rowData.isman!='是'){
                alert("同行人员不允许分配啤酒节门票！")
                return;
            }
            vm.nocBusiness.company = rowData.company;
            vm.nocBusiness.mpMax = rowData.mpMax;
            vm.nocBusiness.mpMax1 = rowData.mpMax1;
            vm.nocBusiness.mpMax2 = rowData.mpMax2;
            vm.showFpseat = true;
            vm.showFpseatAdd = true;
            vm.showList = false;
            vm.showAdd = false;
            console.log("---");
            vm.list2(id);
        },
        getRow: function () {
            $.ajax({
                type: "GET",
                url: baseURL + "noc/nocbeerseat/getRow",
                contentType: "application/json",
                data: {area:vm.area,valid_date:vm.nocBeerSeat.validDate},
                success: function(r){
                    if(r.code == 0){
                        vm.nocRow = r.list;
                    }
                }
            });
        },
        getNum: function () {
            $.ajax({
                type: "GET",
                url: baseURL + "noc/nocbeerseat/getNum",
                contentType: "application/json",
                data: {area:vm.area,table_row:vm.table_row,valid_date:vm.nocBeerSeat.validDate},
                success: function(r){
                    if(r.code == 0){
                        vm.nocNum = r.list;
                    }
                }
            });
        },
        getSeat: function () {
            $.ajax({
                type: "GET",
                url: baseURL + "noc/nocbeerseat/getSeat",
                contentType: "application/json",
                data: {area:vm.area,table_row:vm.table_row,table_num:vm.table_num,valid_date:vm.nocBeerSeat.validDate},
                success: function(r){
                    if(r.code == 0){
                        vm.nocSeat = r.list;
                    }
                }
            });
        },
        getLabels: function () {
            $.ajax({
                type: "POST",
                url: baseURL + "noc/noclabel/list?limit=1000",
                contentType: "application/json",
                data: {},
                success: function(r){
                    if(r.code == 0){
                        vm.nocLabels = r.page.list;
                        //设置默认选中
                        if(vm.selectedLabels){
                            $("input[type='checkbox'][id='cklabel']").each(function() {
                                for(i=0;i<vm.selectedLabels.length;i++){
                                    if($(this).val()==vm.selectedLabels[i].labelId){
                                        $(this).prop('checked',true);
                                    }
                                }
                            });
                        }
                    }
                }
            });
        },
        vlidDateChange:function (event) {
            var el = event.currentTarget;
            console.log(vm.nocBusiness);
            if(el.value=='2019-07-18'){
                vm.mpNum = vm.nocBusiness.mpMax;
            }else if(el.value=='2019-07-19'){
                vm.mpNum = vm.nocBusiness.mpMax1;
            }else if(el.value=='2019-07-20'){
                vm.mpNum = vm.nocBusiness.mpMax2;
            }
        }
    }
});